<template>
  <div class="notification-stack" v-if="notifications.length > 0">
    <!-- 通知头部 -->
    <div class="notification-header">
      <div class="notification-title-bar">
        <span class="notification-title-text">消息通知</span>
        <el-button 
          type="text" 
          size="small" 
          class="clear-all-btn"
          @click="clearAllNotifications"
        >
          全部标记已读
        </el-button>
      </div>
    </div>
    
    <!-- 通知列表容器 -->
    <div class="notification-list-container">
      <!-- 可见的通知列表 -->
      <transition-group name="notification" tag="div" class="notification-wrapper">
        <div 
          v-for="notification in visibleNotifications" 
          :key="notification.id"
          class="notification-item"
          :class="`notification-item--${notification.type}`"
        >
          <!-- 通知图标 -->
          <div class="notification-icon">
            <el-icon :class="`icon--${notification.type}`">
              <component :is="getIconComponent(notification.type)" />
            </el-icon>
          </div>
          
          <!-- 内容 -->
          <div class="notification-content">
            <div class="notification-message">{{ notification.message }}</div>
            <div class="notification-time">{{ formatTime(notification.time) }}</div>
          </div>
          
          <!-- 关闭按钮 -->
          <div class="notification-close" @click.stop="removeNotification(notification.id)">
            <el-icon><Close /></el-icon>
          </div>
        </div>
      </transition-group>
    </div>
    
    <!-- 折叠/展开按钮 -->
    <div 
      v-if="notifications.length > maxVisible" 
      class="notification-toggle" 
      @click="toggleCollapse"
    >
      <span>{{ isCollapsed ? `还有 ${notifications.length - maxVisible} 条消息` : '收起' }}</span>
      <el-icon class="toggle-icon" :class="{ 'rotated': !isCollapsed }"><ArrowDown /></el-icon>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElIcon } from 'element-plus'
import { 
  ArrowDown, 
  Close,
  Check, 
  Warning,
  CircleClose,
  InfoFilled 
} from '@element-plus/icons-vue'

// Props
const props = defineProps({
  maxVisible: {
    type: Number,
    default: 3
  },
  autoCloseDuration: {
    type: Number,
    default: 5000 // 默认5秒自动关闭
  }
})

// 通知列表 - 初始为空，等待真实数据
const notifications = ref([])

// 折叠状态
const isCollapsed = ref(true)

// 可见的通知
const visibleNotifications = computed(() => {
  if (!isCollapsed.value) {
    return notifications.value
  }
  return notifications.value.slice(0, props.maxVisible)
})

// 获取图标组件
const getIconComponent = (type) => {
  const iconMap = {
    success: Check,
    warning: Warning,
    error: CircleClose,
    info: InfoFilled
  }
  return iconMap[type] || InfoFilled
}

// 格式化时间
const formatTime = (timestamp) => {
  const now = Date.now()
  const diff = now - timestamp
  const minutes = Math.floor(diff / 60000)
  
  if (minutes < 1) return '刚刚'
  if (minutes < 60) return `${minutes}分钟前`
  
  const hours = Math.floor(minutes / 60)
  if (hours < 24) return `${hours}小时前`
  
  const days = Math.floor(hours / 24)
  return `${days}天前`
}

// 切换折叠状态
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

// 移除通知
const removeNotification = (id) => {
  const index = notifications.value.findIndex(n => n.id === id)
  if (index > -1) {
    notifications.value.splice(index, 1)
  }
}

// 添加通知（供外部调用）
const addNotification = (notification) => {
  const newNotification = {
    id: Date.now() + Math.random().toString(36).substr(2, 9), // 确保唯一ID
    type: 'info',
    time: Date.now(),
    ...notification
  }
  notifications.value.unshift(newNotification)
  
  // 设置自动关闭定时器
  setTimeout(() => {
    removeNotification(newNotification.id)
  }, props.autoCloseDuration)
  
  // 限制最大通知数量
  if (notifications.value.length > 10) {
    notifications.value.pop()
  }
  
  return newNotification.id
}

// 清空所有通知
const clearAllNotifications = () => {
  notifications.value = []
}

// 暴露方法给父组件
defineExpose({
  addNotification,
  clearAllNotifications,
  removeNotification
})
</script>

<style scoped>
.notification-stack {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2050;
  max-width: 350px;
  width: 100%;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.notification-header {
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  background-color: #fafafa;
}

.notification-title-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notification-title-text {
  font-size: 14px;
  font-weight: 600;
  color: #333;
}

.clear-all-btn {
  font-size: 12px;
  color: #1890ff;
  padding: 0;
}

.clear-all-btn:hover {
  color: #40a9ff;
}

.notification-list-container {
  max-height: 400px;
  overflow-y: auto;
}

.notification-item {
  display: flex;
  align-items: flex-start;
  padding: 12px 16px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-item:hover {
  background-color: #fafafa;
}

.notification-icon {
  margin-right: 12px;
  flex-shrink: 0;
  margin-top: 2px;
}

.notification-icon .el-icon {
  font-size: 18px;
}

.icon--success {
  color: #67c23a;
}

.icon--warning {
  color: #e6a23c;
}

.icon--error {
  color: #f56c6c;
}

.icon--info {
  color: #909399;
}

.notification-content {
  flex: 1;
  min-width: 0;
}

.notification-message {
  font-size: 14px;
  color: #303133;
  line-height: 1.5;
  margin-bottom: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.notification-time {
  font-size: 11px;
  color: #909399;
}

.notification-close {
  margin-left: 8px;
  color: #c0c4cc;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 14px;
  padding: 2px;
  border-radius: 4px;
  transition: all 0.2s ease;
  margin-top: 2px;
}

.notification-close:hover {
  color: #909399;
  background-color: rgba(0, 0, 0, 0.05);
}

.notification-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  border-top: 1px solid #f0f0f0;
  background-color: #fafafa;
  cursor: pointer;
  color: #1890ff;
  font-size: 13px;
  transition: background-color 0.2s ease;
}

.notification-toggle:hover {
  background-color: #f0f0f0;
}

.toggle-icon {
  margin-left: 4px;
  transition: transform 0.3s ease;
}

.toggle-icon.rotated {
  transform: rotate(180deg);
}

/* 通知过渡动画 */
.notification-wrapper {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.notification-enter-active,
.notification-leave-active {
  transition: all 0.3s ease;
  overflow: hidden;
}

.notification-enter-from {
  opacity: 0;
  transform: translateX(100%);
  max-height: 0;
  padding: 0;
  margin: 0;
}

.notification-leave-to {
  opacity: 0;
  transform: translateX(100%);
  max-height: 0;
  padding: 0;
  margin: 0;
}

.notification-enter-to {
  opacity: 1;
  transform: translateX(0);
}

.notification-leave-from {
  opacity: 1;
  transform: translateX(0);
}

.notification-move {
  transition: transform 0.3s ease;
}

/* 滚动条样式 */
.notification-list-container::-webkit-scrollbar {
  width: 4px;
}

.notification-list-container::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.notification-list-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.notification-list-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notification-stack {
    max-width: calc(100% - 40px);
    left: 20px;
    right: 20px;
  }
}
</style>